#batch_rename.py
import os

def remove_substring_from_filenames(directory, num_chars_to_remove):
    # 遍历目录中的所有文件
    for root, dirs, files in os.walk(directory):
        for filename in files:
            # 检查文件名是否包含需要去掉的字符串
            if len(filename) >= num_chars_to_remove:
                # 构建新的文件名
                new_filename = filename[num_chars_to_remove:].strip()
                # 构建完整的旧文件路径和新文件路径
                old_file_path = os.path.join(root, filename)
                new_file_path = os.path.join(root, new_filename)
                # 重命名文件
                os.rename(old_file_path, new_file_path)
                print(f"Renamed: {old_file_path} -> {new_file_path}")

# 用户输入目录和需要去掉的字符串的前几个字符
directory = input("请输入目录路径: ")
num_chars_to_remove = len(input("请输入需要去掉的字符串的前几个字符: "))

# 调用函数进行处理
remove_substring_from_filenames(directory, num_chars_to_remove)
